﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Core.DS;

namespace Web.Controllers
{
    public partial class datastructController : JsonController
    {
        [OnlyTeacherUp]
        public ActionResult getClassList()
        {
            return Json(classes.getClassList());
        }

        [OnlyTeacherUp]
        public ActionResult addOrEditClass(int? cid, string title)
        {
            CheckEmpty(title);
            return Json(classes.addOrEdiClass(cid, title));
        }

        [OnlyTeacherUp]
        public ActionResult getClassInfo(int cid)
        {
            return Json(classes.getClassInfo(cid));
        }

        [OnlyTeacherUp]
        public ActionResult deleteClass(int cid)
        {
            return Json(classes.deleteClass(cid));
        }

        [OnlyTeacherUp]
        public ActionResult getStudentList(int start, int limit, int cid)
        {
            return Json(classes.getStudentList(start, limit, cid));
        }

        [OnlyTeacherUp]
        public ActionResult importStudentList(int cid, string studentList, string cleanFirst)
        {
            CheckWhite(studentList);
            var reader = new System.IO.StringReader(studentList);
            string line;
            int no;
            var nos = new List<int>();
            var sids = new List<string>();
            var names = new List<string>();
            var sexs = new List<bool>();
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim(' ', '　', '\t');
                if (line == "") continue;
                line = line.Replace('　', ' ');
                line = line.Replace('\t', ' ');
                while (line.Contains("  ")) line = line.Replace("  ", " ");
                var datas = line.Split(' ');
                if (datas.Length != 3) continue;
                if (!int.TryParse(datas[0], out no)) continue;
                nos.Add(no);
                sids.Add(datas[1]);
                if (datas[2][datas[2].Length - 1] == '*')
                {
                    names.Add(datas[2].Substring(0, datas[2].Length - 1));
                    sexs.Add(false);
                }
                else
                {
                    names.Add(datas[2]);
                    sexs.Add(true);
                }
            }
            return Json(classes.importStudentList(cid, nos.ToArray(), sids.ToArray(), names.ToArray(), sexs.ToArray(), cleanFirst.ToBool()));
        }

        [OnlyTeacherUp]
        [ParamsNoEmpty]
        public ActionResult deleteStudent(int cid, string sid)
        {
            return Json(classes.deleteStudent(cid, sid));
        }

        [OnlyTeacherUp]
        public ActionResult cleanStudentList(int cid)
        {
            return Json(classes.cleanStudentList(cid));
        }

        [OnlyTeacherUp]
        public ActionResult cmpCode(string sid, double similarity)
        {
            return Json(status.cmpCode(sid, similarity));
        }
    }
}
